package custom_components
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Timer;

	public class EmailExtractor extends EventDispatcher
	{
		public var url:String = '';
		public var emailArray:Array;
		public function EmailExtractor(url:String)
		{
			
			if (url.split('http://').length == 1)
			{
				this.url = 'http://'+url;
			}
			else
			{
				this.url = url;
			}
			emailArray = new Array();
		}
		
		public function findContactPage():void
		{
			var urlloader:URLLoader ;
			urlloader = new URLLoader(new URLRequest(url));
			urlloader.addEventListener(Event.COMPLETE , handleComplete);
			urlloader.addEventListener(IOErrorEvent.IO_ERROR , errorHandler);
		}
		
		protected function handleComplete(event:Event):void
		{
			var string :String = (event.target.data);
			getHref(event.target.data);
			
		}
		
		protected function errorHandler(event:IOErrorEvent):void
		{
			// TODO Auto-generated method stub
			trace('error');
			dispatchEvent(new Event('href_not_found'));
		}
		
		private function getHref(str:String):void
		{
			trace('callinggg');
			var reg:RegExp = new RegExp("(https?://)?(www\\.)?([a-zA-Z0-9_%]*)\\b\\.[a-z]{2,4}(\\.[a-z]{2})?((/[a-zA-‌​Z0-9_%]*)+)?(\\.[a-z]*)?(:\\d{1,5})?","g");
			var hrefArray:Array = str.match(reg);
			trace(hrefArray.length + hrefArray[0]);
			var contact:String = ''
			for (var i:Number = 0 ; i < hrefArray.length ; i++)
			{
				var str:String = hrefArray[i].toString();
				str = str.toLowerCase();
				//hrefArray[i] = hrefArray[i].toString().toLowerCase();
			//	trace('tags are '+ hrefArray[i]);
				if (str.split('contact').length > 1 && str.split('.css').length < 2)
				{
					trace( hrefArray[i]);
					contact = hrefArray[i];
				}
			}
			trace('contact tag is ' + contact);
			contact = contact.split(url).join('');
			if (contact.split('www').length > 1)
			{
				url = '';
			}
			var surl:String = url;
			surl = surl.split('http://').join('');
			surl = surl.split('https://').join('');
			surl = surl.split('/')[0];
			if (contact != '')
			{
				trace('complete url is '+surl+'/'+contact);
				var urlloader:URLLoader = new URLLoader(new URLRequest('http://'+surl+'/'+contact));
				urlloader.addEventListener(Event.COMPLETE , getEmails);
				urlloader.addEventListener(IOErrorEvent.IO_ERROR , function ():void
				{
					var timer:Timer = new Timer(1000,3);
					timer.start();
					timer.addEventListener(TimerEvent.TIMER_COMPLETE , function ():void
					{
						trace('second attempt');
						var urlloader2:URLLoader = new URLLoader(new URLRequest(url+'/'+contact));
						urlloader2.addEventListener(Event.COMPLETE , getEmails);
						urlloader2.addEventListener(IOErrorEvent.IO_ERROR , errorHandler);
					});
					
				}		
				);
			}
			else
			{
				dispatchEvent(new Event('href_not_found'));
			}
		}
		
		protected function getEmails(event:Event):void
		{
			trace('extracting emails');
			var string :String = (event.target.data);
			string = string.replace(/<.*?>/g, " ");
			var reg:RegExp = /\b([^\s]+@[^\s]+)\b/g;
			emailArray = string.match(reg);
			for (var i:Number = 0 ; i < emailArray.length ; i++)
			{
				trace(emailArray[i]);
			}
			
			dispatchEvent(new Event ('email_found'));
		}
	}
}